package main

/**
缓冲池
后续再改进算法
*/
type BufferPool struct {
	Pools      map[int]chan []byte
	bufferSize int
	max        int
}

func NewBufferPool(max int) *BufferPool {
	return &BufferPool{
		max:   max,
		Pools: make(map[int]chan []byte),
	}
}

func (this *BufferPool) Put(bs []byte) {
	pool, ok := this.Pools[len(bs)]
	if !ok {
		return
	}
	select {
	case pool <- bs:
	default:
	}
}

func (this *BufferPool) Get(size int) []byte {
	pool, ok := this.Pools[size]
	if !ok {
		pool = make(chan []byte)
		this.Pools[size] = pool
	}
	select {
	case bs := <-pool:
		return bs
	default:
		return make([]byte, size)
	}
}
